const sequelize = require("./db");
const Admin = require("./models/Admin");
const Banner = require("./models/Banner");
const Blogtype = require("./models/Blogtype");
const Blog = require("./models/Blog");
const Demo = require("./models/Demo");
const Message = require("./models/Message");
const Setting = require("./models/Setting");
const About = require("./models/About");
const md5 = require("md5");

Blogtype.hasMany(Blog, {
    foreignKey: "categoryId",
    as: "category"
});
Blog.belongsTo(Blogtype, {
    foreignKey: "categoryId",
    as: "category"
});

Blog.hasMany(Message, {
    foreignKey: "blogId",
    as: "blog"
});
Message.belongsTo(Blog, {
    foreignKey: "blogId",
    as: "blog"
});

sequelize.sync({
    alter: true
}).then(() => {

    console.log("模型同步成功");
    Admin.count().then((count) => {
        if (count === 0) {
            Admin.create({
                name: "超级管理员",
                loginId: "admin",
                loginPwd: md5("123123")
            }).then(() => {
                console.log("超级管理员添加成功");
            });
        }
    });
    Banner.count().then((count) => {
        if (count === 0) {
            Banner.bulkCreate([{
                "midImg": "/static/images/bg1_mid.jpg",
                "bigImg": "/static/images/bg1_big.jpg",
                "title": "塞尔达旷野之息",
                "description": "2017年年度游戏，期待续作"
            }, {
                "midImg": "/static/images/bg2_mid.jpg",
                "bigImg": "/static/images/bg2_big.jpg",
                "title": "塞尔达四英杰",
                "description": "四英杰里面你最喜欢的又是谁呢"
            }, {
                "midImg": "/static/images/bg3_mid.jpg",
                "bigImg": "/static/images/bg3_big.jpeg",
                "title": "日本街道",
                "description": "动漫中经常出现的日本农村街道，一份独特的恬静"
            }]).then(() => {
                console.log("初始化首页标语成功");
            });
        }
    });

    About.count().then((count) => {
        if (!count) {
            About.create({
                url: "https://oss.duyiedu.com/demo-summary/网页简历/index.html"
            }).then(() => {
                console.log("初始化关于我数据成功");
            });
        }
    });

    Setting.count().then((count) => {
        if (!count) {
            Setting.create({
                avatar: "/static/images/avatar.jpg",
                siteTitle: "张三的小窝",
                github: "",
                qq: "3252667584",
                qqQrCode: "/static/images/qqQrCode.jpg",
                weixin: "KADSDP-0803-01",
                weixinQrCode: "/static/images/wxQrCode.jpg",
                mail: "3252667584@qq.com",
                icp: "",
                githubName: "ZhangShan",
                favicon: "http://mdrs.yuanjin.tech/Fs4CDlC6mwe_WXLMIiXcmSJLHO4f",
            }).then(() => {
                console.log("初始化全局设置成功");
            });
        }
    });
});
